package com.stedu.controller;

import com.stedu.bean.RepairTask;
import com.stedu.bean.RespBean;
import com.stedu.exception.SteduException;
import com.stedu.service.RepairTaskService;
import com.stedu.service.RepairerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@CrossOrigin
@RestController
@RequestMapping("/admin/repairTask")

public class RepairTaskController {
    @Autowired
    private RepairerService repairerService;
    @Autowired
    private RepairTaskService repair_taskService;
    @GetMapping
    public RespBean selectAll(){
        List<RepairTask> projectList = repair_taskService.selectAll();
        projectList.stream().forEach(System.out::println);
        return RespBean.ok("", projectList);
    }
    @GetMapping("/allEmployee/{tid}")
    public RespBean allEmployee(@PathVariable("tid") Integer tid){
        System.out.println(repairerService.selectByRname(null));
        List<Map<String, Object>> repairerMapList = repairerService.selectByRname(null)
                .stream()
                .filter(e -> e.getEstate() == 1)
                .map(e -> {
                    Map<String, Object> repairerMap = new HashMap<>();
                    repairerMap.put("key", e.getRid());
                    repairerMap.put("label", e.getDepartment().getDname() + "-" + e.getRname());
                    return repairerMap;
                }).collect(Collectors.toList());
        //获取已经分配的员工的rid
        List<Long> rids = repair_taskService.selectRidByTid(tid);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("allEmployee",repairerMapList);
        map.put("selectedRids", rids);
        return RespBean.ok("",map);
    }
     @PostMapping("/tidAndRid")
    public RespBean insertTidAndRid(@Validated @RequestBody Map<String,Object> map ){
        Integer tid = (Integer) map.get("tid");
        List<Integer> ridsList = (List<Integer>)map.get("rids");
        Long[] rids = new Long[ridsList.size()];
        for (int i = 0;i < ridsList.size(); i++){
            int rid = ridsList.get(i);
            rids[i] = new Long(rid + "");
        }
         repair_taskService.insertTidAndRid(tid,rids);
        return  RespBean.ok("分配员工成功");
    }
    @PostMapping
    public RespBean insert(@Validated @RequestBody RepairTask repairTask) throws SQLException, SteduException {


        if (repair_taskService.insert(repairTask)) {
            return RespBean.ok("添加成功");

        } else {
            return RespBean.error("添加失败，请重新添加");


        }
    }
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public RespBean update(@Validated @RequestBody RepairTask repairTask) throws SQLException, SteduException {
        if (repair_taskService.update(repairTask)) {
            return RespBean.ok("修改成功~~~");
        } else {
            return RespBean.error("修改失败，请重新修改");
        }
    }
    @GetMapping("/{tid}")
    public RespBean selectByTid(@PathVariable("tid") Integer tid) throws SQLException {
        RepairTask t = repair_taskService.selectByTid(tid);
        return RespBean.ok("", t);
    }
    @DeleteMapping("/{tid}")
    public RespBean delete(@PathVariable("tid") Integer tid) throws SQLException, SteduException {
        if (repair_taskService.delete(tid)) {
            return RespBean.ok("修缮成功~~~");
        } else {
            return RespBean.error("修缮失败，请联系管理员");
        }
    }
    @DeleteMapping("/employee/{rid}")
    public RespBean handleEmployeeResignation(@PathVariable("rid") Integer rid) {
        try {
            repair_taskService.deleteRidAndByTid(rid);
            return RespBean.ok("员工离职处理成功，关联关系已删除");
        } catch (Exception e) {
            return RespBean.error("员工离职处理失败：" + e.getMessage());
        }
    }
}

